<?php

namespace app\model;

use app\model\Theme as Theme_mod;
use think\Db;
use think\Model;

class Modules extends Model
{
    protected $table = 'yx_sys_modules';
    protected $pk = 'id';

    public function getSofttype($key = '')
    {

        $softType = [
            'ruanjian2020' => ['name' => '拼团2020', 'type' => 'soft', 'sql' => " system->>'$.ruanjian' = '2' and system->>'$.is_mystery_boxes' = '1' and system->>'$.is_mystery_boxes_feichi' != '1' and system->>'$.is_mother_boxes'!= '1' "],
            'ruanjian2019' => ['name' => '拼团2019', 'type' => 'soft', 'sql' => " system->>'$.ruanjian' = '0' "],
            'tuanjia2' => ['name' => '2个团价', 'type' => 'soft', 'sql' => " system->>'$.ruanjian' = '42' "],
            'tuanjia3' => ['name' => '3个团价', 'type' => 'soft', 'sql' => " system->>'$.ruanjian' = '43' "],
            'weituan' => ['name' => '微团', 'type' => 'soft', 'sql' => " system->>'$.ruanjian' = '1' "],
            'bargain' => ['name' => '砍价', 'type' => 'soft', 'sql' => " system->>'$.ruanjian' = '5' and system->>'$.is_multiple_bargain' != 1 "],
            'multiple_bargain' => ['name' => '多产品砍价', 'type' => 'soft', 'sql' => " system->>'$.ruanjian' = '5' and system->>'$.is_multiple_bargain' = 1 "],
            'mystery_boxes' => ['name' => '盲盒', 'type' => 'soft', 'sql' => " system->>'$.is_mystery_boxes' = '1' "],
            'feichi_mystery_boxes' => ['name' => '多产品砍价', 'type' => 'soft', 'sql' => " system->>'$.is_mystery_boxes_feichi' = '1' "],
            'mother_boxes' => ['name' => '妈妈宝盒', 'type' => 'soft', 'sql' => " system->>'$.is_mother_boxes' = '1' "],
            'weike' => ['name' => '微客', 'type' => 'soft', 'sql' => " system->>'$.ruanjian' = '10' "],
            'jiameng' => ['name' => '加盟', 'type' => 'soft', 'sql' => " system->>'$.ruanjian' = '0' "],
            'multiple_merchants' => ['name' => '异业', 'type' => 'soft', 'sql' => " system->>'$.is_multiple_merchants' = '1' "],

            'fenxiao01' => ['name' => '一级分销', 'type' => 'hb', 'sql' => " system->>'$.fenxiao01' = '1' "],
            'fenxiao02' => ['name' => '二级分销', 'type' => 'hb', 'sql' => " system->>'$.fenxiao01' = '2' "],
            'fenxiao03' => ['name' => '三级分销', 'type' => 'hb', 'sql' => " system->>'$.fenxiao01' = '3' "],
            'zhuanfa' => ['name' => '转发红包', 'type' => 'hb', 'sql' => " system->>'$.fenxiao04' = '1' "],
            'yyy_gift' => ['name' => '摇一摇红包', 'type' => 'hb', 'sql' => " system->>'$.fenxiao07' = '1' "],
            'yyy_hb' => ['name' => '摇一摇红包', 'type' => 'hb', 'sql' => " system->>'$.fenxiao071' = '1' "],
            'worker_tc' => ['name' => '员工提成', 'type' => 'hb', 'sql' => " system->>'$.fenxiao06' = '1' "],
            'read_hb' => ['name' => '阅读红包', 'type' => 'hb', 'sql' => " system->>'$.fenxiao05' = '1' "],
            'other_hb' => ['name' => '额外红包', 'type' => 'hb', 'sql' => " system->>'$.fenxiao08' = '1' "],

            'ispresale' => ['name' => '预售', 'type' => 'gongneng', 'sql' => " system->>'$.ispresale' = '1' "],
            'selecet_pay' => ['name' => '多选支付', 'type' => 'gongneng', 'sql' => " system->>'$.selecet_pay' = '1' "],
            'jiage1' => ['name' => '2个价格', 'type' => 'gongneng', 'sql' => " system->>'$.jiage' = '1' "],
            'jiage2' => ['name' => '3个价格', 'type' => 'gongneng', 'sql' => " system->>'$.jiage' = '2' "],
            'selling_agent' => ['name' => '代理', 'type' => 'gongneng', 'sql' => " system->>'$.selling_agent' = '2' "],
            'lottery' => ['name' => '九宫格抽奖', 'type' => 'gongneng', 'sql' => " system->>'$.lottery' = '1' "],
            'daiyan' => ['name' => '代言', 'type' => 'gongneng', 'sql' => " system->>'$.daiyan' = '1' "],
            'is_shop_plan' => ['name' => '线上预约', 'type' => 'gongneng', 'sql' => " system->>'$.is_shop_plan' = '1' "],
            'redrain_dk' => ['name' => '红包雨抵扣', 'type' => 'gongneng', 'sql' => " system->>'$.redrain' = '1' "],
            'redrain_tx' => ['name' => '功能', 'type' => 'gongneng', 'sql' => " system->>'$.redrain' = '2' "],
        ];
        if ($key) {
            return $softType[$key];
        } else {
            return $softType;
        }

    }

    public function getModulesTypeData()
    {
        $softType = $this->getSofttype();
        $field = [];
        foreach ($softType as $key => $value) {
            $field [] = "count(case when {$value['sql']} then 1  end) as {$key}";
        }
        $data = $this->field($field)->find()->toArray();
        $group_array = [
            'soft' => ['name' => '软件', 'list' => []],
            'hb' => ['name' => '红包', 'list' => []],
            'gongneng' => ['name' => '功能', 'list' => []],
        ];

        foreach ($softType as $index => $item) {
            if ($data[$index]) {
                $count = $data[$index];
            } else {
                $count = 0;
            }
            $group_array[$item['type']]['list'][] = [
                'name' => $item['name'],
                'field' => $index,
                'count' => $count
            ];
        }
        return $group_array;
    }

    // 0取所有小程序和公众号 1微信小程序 2微信公众号
    public function getPlatformAll($platform_type = 1, $keyfield = 'appid')
    {
        if ($platform_type == 0) {
            $sql = "select *,id as uniacid from yx_sys_platform where status=1  order by id desc";
        } else {
            $sql = "select *,id as uniacid from yx_sys_platform where status=1 and platform_type = " . $platform_type . " order by id desc";
        }
        return db_select($sql, [], $keyfield);
    }

    public function getPlatform($id)
    {
        $sql = "select *,id as uniacid from yx_sys_platform where id = '" . $id . "' order by id desc";
        return db_find($sql);
    }

    public function getPlatformByAppid($appid)
    {
        $sql = "select *,id as uniacid from yx_sys_platform where appid = " . $appid . " order by id desc";
        return db_find($sql);
    }

    // 小程序对接活动活动数量
    public function getWxappListAndCount()
    {
        $wxapp_list = Modules::getPlatformAll($platform_type = 1, 'appid');
        $wxapp_list = array_map(function ($item) {
            return array_merge($item, ['used' => 0]);
        }, $wxapp_list);
        $moduleslist = Modules::getModulesAll();
        foreach ($moduleslist as $index => $item) {
            if (($item['system']['softmode'] == 1 || $item['system']['softmode'] == 2) && $item['system']['wxapp'] > 0) {
                $wxapp_list[$item['system']['appidxcx']]['used'] = intval($wxapp_list[$item['system']['appidxcx']]['used']) + 1;
            }
        }
        return $wxapp_list;
    }

    // 公众平台数量
    public function getMpListAndCount()
    {
        $mp_list = Modules::getPlatformAll($platform_type = 2, 'appid');
        $mp_list = array_map(function ($item) {
            return array_merge($item, ['used' => 0]);
        }, $mp_list);
        $moduleslist = Modules::getModulesAll();
        foreach ($moduleslist as $index => $item) {
            if (($item['system']['softmode'] == 1 || $item['system']['softmode'] == 0) && $item['mpid'] > 0) {
                if ($mp_list[$item['system']['appidxcx']]) {
                    $mp_list[$item['system']['appidxcx']]['used'] = intval($mp_list[$item['system']['appidxcx']]['used']) + 1;
                }
            }
        }
        return $mp_list;
    }

    public function getModulesByThemeid($themeid)
    {
        $modules = db_get('yx_sys_modules', array('module_name' => MODULE_NAME, 'themeid' => $themeid));
        $modules['system'] = empty($modules['system']) ? [] : json_decode($modules['system'], true);
        $modules['theme'] = empty($modules['theme']) ? [] : json_decode($modules['theme'], true);
        $modules['config'] = empty($modules['config']) ? [] : json_decode($modules['config'], true);
        return $modules;
    }

    public function getSystemByThemeid($themeid)
    {
        return self::getModulesByThemeid($themeid)['system'];
    }

    public function getModulesAll($Status = -1)
    {
        global $_G;
        $sql = "SELECT * FROM `yx_sys_modules` where theme->>'$.status' <> $Status ORDER BY id asc";
        $modules = db_select($sql, [], 'themeid');
        foreach ($modules as $index => $row) {
            $modules[$index]['system'] = json_decode($row['system'], true);
//            $modules[$index]['system']['s'] = $_G['system']['s'];
//            $modules[$index]['theme'] = json_decode($row['theme'], true);
//            $modules[$index]['config'] = json_decode($row['config'], true);
        }
        return $modules;
    }

    public function getCurrentModuleAllTheme($Status = -1)
    {
        global $_G;
        $sql = "SELECT * FROM `yx_sys_modules` where module_name ='" . MODULE_NAME . "' and theme->>'$.status' <> $Status ORDER BY id asc";
        $modules = db_select($sql, [], 'themeid');
        foreach ($modules as $index => $row) {
            $modules[$index]['system'] = json_decode($row['system'], true);
            $modules[$index]['system']['s'] = $_G['system']['s'];
            $modules[$index]['theme'] = json_decode($row['theme'], true);
            $modules[$index]['config'] = json_decode($row['config'], true);
        }
        return $modules;
    }

    public function UpdateMpid($themeid, $mpid)
    {
        global $_G;
        if (isset($mpid) && $mpid > 0) {
            Theme::update($themeid, ['uniacid' => $mpid]);
            $module = self::getModulesByThemeid($themeid);
            $module['theme']['uniacid'] = $mpid;
            $data = array(
                'mpid' => $mpid,
                'theme' => $module['theme'],
            );
            $_G['mpid'] = $mpid;
            return self::UpdateModulesByThemeid($module['themeid'], $data);
        }
    }

    public function SyncModulesThemeAndConfig($themeid)
    {
        $theme = Theme_mod::getById($themeid);
        $config = Theme_mod::getConfig($themeid, true);
        $data = array(
            'theme' => $theme,
            'config' => $config,
        );
        self::UpdateModulesByThemeid($themeid, $data);
    }

    public function UpdateModulesByThemeid($themeid, $data)
    {
        $module = self::getModulesByThemeid($themeid);
        if (isset($data['system']) && is_array($data['system'])) {
            $data['system'] = array_merge($module['system'], $data['system']);
            $data['system'] = json_encode($data['system']);
            $data['update_time'] = time();
        }
        if (isset($data['config']) && is_array($data['config'])) {
            $data['config'] = array_merge($module['config'], $data['config']);
            $data['config'] = json_encode($data['config']);
            $data['update_time'] = time();
        }
        if (isset($data['theme']) && is_array($data['theme'])) {
            $data['softkey'] = $data['theme']['softkey'];
            $data['theme'] = array_merge($module['theme'], $data['theme']);
            $data['theme'] = json_encode($data['theme']);
            $data['update_time'] = time();
        }
        return db_update("yx_sys_modules", $data, array('module_name' => MODULE_NAME, 'themeid' => $themeid));
    }

    public function getSoftTips($system)
    {
        if (!$system) {
            return '';
        }
        if ($system['ruanjian'] == 1) {
            $softtips = "微团";
        } elseif ($system['is_multiple_bargain']) {
            $softtips = "多产品砍价";
        } elseif ($system['is_mystery_boxes']) {
            $softtips = "盲盒";
        } elseif ($system['is_mystery_boxes_feichi']) {
            $softtips = "飞驰盲盒";
        } elseif ($system['is_mother_boxes']) {
            $softtips = "妈妈宝盒";
        } elseif ($system['ruanjian'] == 2) {
            $softtips = "拼团2020";
        } elseif ($system['ruanjian'] == 5) {
            $softtips = "砍价";
        } elseif ($system['ruanjian'] == 0 && $system['softstyle'] == 5) {
            $softtips = "加盟";
        } elseif ($system['ruanjian'] == 10) {
            $softtips = "微客";
        } elseif ($system['ruanjian'] == 42) {
            $softtips = "2个团价";
        } elseif ($system['ruanjian'] == 43) {
            $softtips = "3个团价";
        } elseif ($system['ruanjian'] == 0) {
            $softtips = "拼团2019";
        }
        if ($system['is_multiple_merchants']) {
            $softtips .= "-异业";
        }
        if ($system['selling_agent']) {
            $softtips .= "-代理";
        }

        if ($system['jiage'] == 1) {
            $softtips .= "-2个价格";
        } elseif ($system['jiage'] == 2) {
            $softtips .= "-3个价格";
        }
        if ($system['lottery']) {
            $softtips .= "-九宫格抽奖";
        }
        if ($system['is_shop_plan']) {
            $softtips .= "-线上预约";
        }
        if ($system['daiyan']) {
            $softtips .= "-代言";
        }
        if ($system['redrain'] == 1) {
            $softtips .= "-红包雨抵扣";
        }
        if ($system['redrain'] == 2) {
            $softtips .= "-红包雨提现";
        }
        if ($system['selecet_pay'] == 1) {
            $softtips .= "-多选支付";
        }
        if ($system['ispresale'] == 1) {
            $softtips .= "-预售";
        }

        if ($system['fenxiao01'] == 3) {
            $softtips .= "_三级分销";
        } elseif ($system['fenxiao01'] == 2) {
            $softtips .= "_二级分销";
        } elseif ($system['fenxiao01'] == 1) {
            $softtips .= "_一级分销";
        }

        if ($system['fenxiao04'] == 1) {
            $softtips .= "-转发红包";
        }
        if ($system['fenxiao07'] == 1) {
            $softtips .= "-摇一摇红包";
        }
        if ($system['fenxiao071'] == 1) {
            $softtips .= "-摇一摇礼品";
        }
        if ($system['fenxiao06'] == 1) {
            $softtips .= "-员工提成";
        }
        if ($system['fenxiao05'] == 1) {
            $softtips .= "-阅读红包";
        }
        if ($system['fenxiao08'] == 1) {
            $softtips .= "-额外红包";
        }
        return $softtips;
    }

    public function getList($sort)
    {
        global $_P;
        $pageNumber = max(1, intval($_P['page']));
        $pageSize = 20;
        $where = ' id > 0';
        $param = input('param.');
        $where .= " and (theme->>'$.status' <> '-1' || theme->>'$.id' is null)";
        if (isset($param['keyword']) && $param['keyword']) {
            $keyword = $param['keyword'];
            $where .= " and module_name like '%{$keyword}'";
        }

        if (isset($param['wxapp']) && $param['wxapp']) {
            $wxapp = $param['wxapp'];
            $where .= " and json_unquote(json_extract(system,'$.wxapp')) = {$wxapp}";
        }
        $nowTime = time();
        if (isset($param['datatype']) && $param['datatype']) {
            switch ($param['datatype']) {
                case 'going_num':
                    $where .= " and json_unquote(json_extract(theme, '$.endtime')) > {$nowTime} and json_unquote(json_extract(theme, '$.status')) >=0";
                    break;
                case 'wxapp_num':
                    $where .= " and json_unquote(json_extract(system, '$.softmode')) = 1";
                    break;
                case 'void_num':
                    $where .= " and json_unquote(json_extract(theme,'$.title')) is null";
                    break;
            }
        }

        if (isset($param['softmode']) && $param['softmode']) {
            $softmode = $this->getSofttype($param['softmode']);
            if ($softmode) {
                $sql = $softmode['sql'];
                $where .= " and {$sql}";
            }
        }

        $list = $this->field(['id', 'module_name', 'mpid', 'themeid',
            'json_unquote(json_extract(theme, "$.title")) as title',   //获取主题标题
            'json_unquote(json_extract(theme, "$.fanscount")) as fanscount',
            'json_unquote(json_extract(theme, "$.scansum")) as scansum',
            'json_unquote(json_extract(system, "$.softmode")) as softmode',
            'json_unquote(json_extract(theme, "$.ordercount")) as allfee',
            'json_unquote(json_extract(theme, "$.commcount")) as fenxiao',
            'json_unquote(json_extract(theme, "$.xiaoshou")) as xiaoshou',
            'json_unquote(json_extract(theme, "$.kefu")) as kefu',
            'system'])
            ->where($where)
            ->order("{$sort}")
            ->paginate($pageSize, false, ['page' => $pageNumber]);

        $modules = Modules::getPlatformAll(0, 'id');
        foreach ($list as $k => $v) {
            $system = json_decode($v['system'], true);
            $list[$k]['soft_tips'] = $this->getSoftTips($system);
            $list[$k]['softmode_tip'] = '';
            if ($v['softmode'] > 0) {
                $wxapp = $system['wxapp'];
                if ($wxapp) {
                    $txt = str_replace('【新版本】', '-新', $modules[$wxapp]['name']);
                    $list[$k]['softmode_tip'] = '<span class="label label-success">' . $txt . '</span>';
                }
            }
            if ($v['softmode'] != 1 && DOMAIN1 == 'www.yixuan.net') {
                if ($v['mpid']) {
                    $txt = '<span class="label label-info">' . $modules[$v['mpid']]['name'] . '</span>';
                    $list[$k]['softmode_tip'] = empty($list[$k]['softmode_tip']) ? $txt : $list[$k]['softmode_tip'] . "<br>" . $txt;
                }
            }

            if ($v['xiaoshou'] == 'null') {
                $list[$k]['xiaoshou'] = '';
            }
            if ($v['kefu'] == 'null') {
                $list[$k]['kefu'] = '';
            }
            unset($list[$k]['system']);
        }

        return $list;
    }

    public function addModules()
    {
        global $_G;
        if ($_G['system']['s']['isaddtheme'] != 1) {
            return_tips('没有操作权限');
        }
        $result = $this->getsoftname();
        if ($result['error'] == 1) {
            return_tips($result['msg']);
        }
        if (empty($result['softname'])) {
            return_tips('获取编号失败请重试');
        }
        for ($i = 0; $i < 10; $i++) {
            $newfile = $result['softname'] . $i;
            $this->createModules($newfile);
            $this->executeSql($newfile);
        }
        $res_url = createWebUrl('yxmodule', array('op' => 'display', 'datatype' => 'void_num'));
        return_tips('添加成功', $res_url);
    }

    public function createModules($name)
    {

        $data = [
            'module_name' => $name,
            'themeid' => 1,
            'mpid' => 0,
        ];
        $this->insert($data);
    }

    public function getsoftname($do = 'new')
    {
        $api_url = 'http://oa.yixuan.net/app/index.php?i=2&c=entry&do=getbanklist&m=yx99999&op=newsoft';
        $postdata = array(
            'server' => DOMAIN0,
            'op2' => $do,
        );
        // Init the CURL session
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $api_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);            // No header in the result
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
        curl_setopt($ch, CURLOPT_POST, 1);              // This is a POST request
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        // Fetch and return content
        $data = curl_exec($ch);
        curl_close($ch);
        // Do something with the result. Here, we echo the long URL
        $data = json_decode($data, true);
        //$shorturl = str_replace("http://","",$data->shorturl);
        return $data['message'];
    }

    //数据库安装，支持老版本的安装与新版本
    public function executeSql($modulename = 'yx18900')
    {
        $sql = "
            CREATE TABLE IF NOT EXISTS `yx_yx18900_aworkerreport` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) NOT NULL,
              `themeid` int(11) NOT NULL,
              `workerid` int(11) NOT NULL,
              `riqi` date DEFAULT NULL,
              `hour` datetime DEFAULT NULL,
              `renewtime` int(11) DEFAULT '0',
              `newfans` int(11) DEFAULT '0',
              `record` int(11) DEFAULT '0',
              `recordzx` int(11) DEFAULT '0',
              `recordxx` int(11) DEFAULT '0',
              `fee` decimal(10,2) DEFAULT '0.00',
              `comm` decimal(10,2) DEFAULT '0.00',
              `scansum` int(11) DEFAULT '0',
              `forward` int(11) DEFAULT '0',
              `refundfee` decimal(10,2) DEFAULT '0.00',
              `refund` int(11) DEFAULT '0',
              `workerid_old` int(11) DEFAULT '0',
             PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
            ALTER TABLE `yx_yx18900_aworkerreport` ADD INDEX rds_idx_0 (`uniacid`, `themeid`, `workerid`, `riqi`);


            CREATE TABLE IF NOT EXISTS `yx_yx18900_aposter` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `uniacid` int(11) NOT NULL,
            `themeid` int(11) NOT NULL DEFAULT '0',
            `title` varchar(100) DEFAULT NULL,
            `keyword` varchar(100) NOT NULL,
            `bg` varchar(300) DEFAULT NULL,
            `data` text,
            `status` int(2) NOT NULL,
            `isbg` int(2) NOT NULL,
            `waittips` varchar(100) DEFAULT NULL,
            `poster_tips1` varchar(100) DEFAULT NULL,
            `poster_tips2` varchar(100) DEFAULT NULL,
             PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

            -- CREATE TABLE IF NOT EXISTS `yx_yx18900_acode` (
            -- `id` int(11) NOT NULL AUTO_INCREMENT,
            -- `uniacid` int(11) DEFAULT '0',
            -- `themeid` int(11) DEFAULT '0',
            -- `nickname` varchar(300) DEFAULT NULL,
            -- `avatar` varchar(300) DEFAULT NULL,
            -- `openid` varchar(32) DEFAULT NULL,
            -- `uid` int(11) NOT NULL DEFAULT '0',
            -- `status` int(2) DEFAULT '0',
            -- `recordid` int(11) NOT NULL DEFAULT '0',
            -- `code` varchar(100) NOT NULL,
            -- `pwd` varchar(100) NOT NULL,
            -- `tips` varchar(300) NOT NULL,
            -- `ing` varchar(100) NOT NULL,
            -- `end` varchar(100) NOT NULL,
            -- `uid_old` int(11) DEFAULT '0',
            -- `openid_old` varchar(32) DEFAULT NULL,
            --  PRIMARY KEY (`id`)
            -- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;


            CREATE TABLE IF NOT EXISTS `yx_yx18900_ascan` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) DEFAULT '0',
              `themeid` int(11) DEFAULT '0',
              `openid` varchar(32) DEFAULT NULL,
              `uid` int(11) DEFAULT '0',
              `nickname` varchar(300) DEFAULT NULL,
              `avatar` varchar(300) DEFAULT NULL,
              `fromuid` int(11) DEFAULT '0' COMMENT '上级uid',
              `fopenid` varchar(32) DEFAULT NULL COMMENT '上级openid',
              `workerid` int(11) DEFAULT '0',
              `status` int(2) DEFAULT '0' COMMENT '状态',
              `ischeck` int(2) DEFAULT '0',
              `addtime` int(11) DEFAULT '0',
              `uid_old` int(11) DEFAULT '0',
              `openid_old` varchar(32) DEFAULT NULL,
              `fromuid_old` int(11) DEFAULT '0',
              `fopenid_old` varchar(32) DEFAULT NULL,
              `workerid_old` int(11) DEFAULT '0',
             PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

            CREATE TABLE IF NOT EXISTS `yx_yx18900_afriend` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) DEFAULT '0',
              `themeid` int(11) DEFAULT '0',
              `openid` varchar(32) DEFAULT NULL,
              `uid` int(11) DEFAULT '0',
              `nickname` varchar(300) DEFAULT NULL,
              `avatar` varchar(300) DEFAULT NULL,
              `fromuid` int(11) DEFAULT '0' COMMENT '上级uid',
              `fopenid` varchar(32) DEFAULT NULL COMMENT '上级openid',
              `workerid` int(11) DEFAULT '0',
              `status` int(2) DEFAULT '0' COMMENT '状态',
              `ischeck` int(2) DEFAULT '0',
              `addtime` int(11) DEFAULT '0',
              `uid_old` int(11) DEFAULT '0',
              `openid_old` varchar(32) DEFAULT NULL,
              `fromuid_old` int(11) DEFAULT '0',
              `fopenid_old` varchar(32) DEFAULT NULL,
              `workerid_old` int(11) DEFAULT '0',

             PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

            CREATE TABLE IF NOT EXISTS `yx_yx18900_arelation` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) DEFAULT '0',
              `themeid` int(11) DEFAULT '0',
              `openid` varchar(32) DEFAULT NULL,
              `uid` int(11) DEFAULT '0',
              `nickname` varchar(300) DEFAULT NULL,
              `avatar` varchar(300) DEFAULT NULL,
              `parentid` int(11) DEFAULT '0' COMMENT '上级uid',
              `popenid` varchar(32) DEFAULT NULL COMMENT '上级openid',
              `status` int(2) DEFAULT '0' COMMENT '状态',
              `isfrozen` int(2) DEFAULT '0' COMMENT '是否锁定上一级',
              `isorder` int(2) DEFAULT '0' COMMENT '支付后不再更新所属员工',
              `workerid` int(11) DEFAULT '0',
              `ischeck` int(2) DEFAULT '0',
              `addtime` int(11) DEFAULT '0',
              `uid_old` int(11) DEFAULT '0',
              `openid_old` varchar(32) DEFAULT NULL,
              `parentid_old` int(11) DEFAULT '0',
              `popenid_old` varchar(32) DEFAULT NULL,
              `workerid_old` int(11) DEFAULT '0',
             PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;


            -- CREATE TABLE  IF NOT EXISTS `yx_yx18900_afake` (
            --   `id` int(11) NOT NULL AUTO_INCREMENT,
            --   `uniacid` int(11) DEFAULT '0',
            --   `themeid` int(11) DEFAULT '0',
            --   `nickname` varchar(300) DEFAULT NULL,
            --   `avatar` varchar(300) DEFAULT NULL,
            --   `openid` varchar(32) DEFAULT NULL,
            --   `groupid` int(2) DEFAULT '0',
            --   `type` int(2) DEFAULT '-1',
            --   `status` int(2) DEFAULT '0',
            --   PRIMARY KEY (`id`)
            -- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

            CREATE TABLE  IF NOT EXISTS `yx_yx18900_acomm` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) DEFAULT '0',
              `ordersn` varchar(100) DEFAULT NULL,
              `themeid` int(11) DEFAULT '0',
              `recordid` int(11) DEFAULT '0',
              `type` int(2) DEFAULT '0' COMMENT '0开团红包 1参团红包 2二级红包 3三级红包 4 转发红包 5阅读红包 6员工提成 7摇一摇红包 8补余款 9单买 10进入链接获得 11自已报名获得 12代理提成 13抽奖红包提现 15额外红包 98待提现 99手动提现',
              `openid` varchar(32) DEFAULT NULL,
              `uid` int(11) DEFAULT '0',
              `fee` decimal(10,2) DEFAULT '0.00',
              `addtime` int(11) DEFAULT '0',
              `sendtime` int(11) DEFAULT '0',
              `status` int(2) DEFAULT '0',
              `tiaozhuan` int(2) NOT NULL,
              `fromuid` int(11) DEFAULT '0',
              `fopenid` varchar(32) DEFAULT NULL,
              `workerid` int(11) DEFAULT '0',
              `city` varchar(300) DEFAULT NULL,
              `ip` varchar(30) DEFAULT NULL,
              `msg` varchar(100) DEFAULT NULL,
              `uid_old` int(11) DEFAULT '0',
              `openid_old` varchar(32) DEFAULT NULL,
              `fromuid_old` int(11) DEFAULT '0',
              `fopenid_old` varchar(32) DEFAULT NULL,
              `workerid_old` int(11) DEFAULT '0',
              PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

            CREATE TABLE IF NOT EXISTS  `yx_yx18900_acommshare` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) DEFAULT '0',
              `themeid` int(11) DEFAULT '0',
              `type` int(2) DEFAULT '0' COMMENT '0开团红包 1参团红包 2二级红包 3三级红包 4 转发红包 5阅读红包 6员工提成 9单买 10进入链接获得 11自已报名获得 99手动提现',
              `openid` varchar(32) DEFAULT NULL,
              `uid` int(11) DEFAULT '0',
              `fee` decimal(10,2) DEFAULT '0.00',
              `fromuid` int(11) DEFAULT '0',
              `addtime` int(11) DEFAULT '0',
              `sendtime` int(11) DEFAULT '0',
              `status` int(2) DEFAULT '0' COMMENT '0未支付 1已支付 2不在活动地区 3今日红包已发完 4同一网络到上限',
              `city` varchar(30) DEFAULT NULL COMMENT '5离下次领取 6每天只可领 7您总计可领 8一小时内交易未达到红包要求',
              `ip` varchar(30) DEFAULT NULL,
              `msg` varchar(100) DEFAULT NULL,
              `uid_old` int(11) DEFAULT '0',
              `openid_old` varchar(32) DEFAULT NULL,
              `fromuid_old` int(11) DEFAULT '0',
              PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

            CREATE TABLE IF NOT EXISTS `yx_yx18900_atheme` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) NOT NULL,
              `uid` int(11) DEFAULT '0',
              `title` varchar(100) NOT NULL,
              `desc` varchar(100) NOT NULL,
              `softsn` varchar(100) NOT NULL,
              `softsn_status` int(2) DEFAULT '0',
              `img` varchar(300) NOT NULL,
              `addimg` varchar(300) NOT NULL,
              `addimgxcx` varchar(300) NOT NULL,
              `headimg` varchar(300) NOT NULL,
              `indexno` int(11) NOT NULL DEFAULT '0',
              `scansum` int(11) NOT NULL DEFAULT '0',
              `forward` int(11) DEFAULT '0',
              `keyword` varchar(50) DEFAULT NULL,
              `mobile` varchar(50) NOT NULL,
              `begintime` int(11) DEFAULT '0',
              `endtime` int(11) DEFAULT '0',
              `status` int(2) DEFAULT '0',
              `mobileset` int(2) DEFAULT '0',
              `jiezhang` varchar(100) NOT NULL,
              `softkey` varchar(32) NOT NULL,
              `chongzhi` decimal(10,2) DEFAULT '0.00',
              `refundfee` int(11) DEFAULT '0',
              `isautorefund` int(2) DEFAULT '0',
              `banmian` int(2) DEFAULT '0',
              `fanscount` int(11) DEFAULT '0',
              `recordcount` int(11) DEFAULT '0',
              `ordercount` decimal(10,2) DEFAULT '0.00',
              `commcount` decimal(10,2) DEFAULT '0.00',
              `blacklist` text NOT NULL,
              `whitelist` text NOT NULL,
              `softqubanquan` int(2) DEFAULT '0',
              `softoem` int(2) DEFAULT '0',
              `yixuanbanquan` int(2) DEFAULT '0',
              `USERURL` varchar(300) NOT NULL,
              `USERJISHU` varchar(300) NOT NULL,
              `USERCOMPANYNAME` varchar(100) NOT NULL,
              `jubf` varchar(100) NOT NULL,
              `qrimg_addworker` varchar(300) NOT NULL,
              `laoshi` varchar(32) NOT NULL,
              `xiaoshou` varchar(32) NOT NULL,
              `kefu` varchar(32) NOT NULL,
              `viewbilltime` int(11) DEFAULT '0',
              PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

            CREATE TABLE IF NOT EXISTS  `yx_yx18900_aconfig` (
             `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) NOT NULL,
              `aid` int(11) NOT NULL,
              `key` varchar(64) NOT NULL,
              `value` text NOT NULL,
              PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;



            CREATE TABLE  IF NOT EXISTS `yx_yx18900_arecord` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) DEFAULT '0',
              `uid` int(11) DEFAULT '0',
              `uid_other` int(11) DEFAULT '0',
              `themeid` int(11) DEFAULT '0',
              `billcode` varchar(100) DEFAULT NULL,
              `nickname` varchar(300) DEFAULT NULL,
              `avatar` varchar(300) DEFAULT NULL,
              `username` varchar(50) NOT NULL,
              `mobile` varchar(50) NOT NULL,
              `openid` varchar(32) DEFAULT NULL,
              `fee` decimal(10,2) DEFAULT '0.00' COMMENT '金额',
              `workerid` int(11) DEFAULT '0',
              `option` varchar(300) NOT NULL,
              `jiagetype` varchar(300) NOT NULL,
              `giftids` varchar(300) DEFAULT NULL,
              `address` varchar(300) DEFAULT NULL,
              `formtwo` varchar(300) DEFAULT NULL,
              `datetime` varchar(100) NOT NULL,
              `addinfo` varchar(100) NOT NULL,
              `fromuid` int(11) DEFAULT '0',
              `fopenid` varchar(32) DEFAULT NULL,
              `isrefund` int(2) DEFAULT '0' COMMENT '1手工退2自动退成功3自动退失败',
              `status` int(2) DEFAULT '0' COMMENT '付款状态',
              `addtime` int(11) DEFAULT '0',
              `refundtime` int(11) DEFAULT '0',
              `isluck` int(2) DEFAULT '0',
              `code` varchar(50) DEFAULT NULL,
              `uncode` varchar(100) NOT NULL,
              `type` int(2) NOT NULL DEFAULT '0' COMMENT '0团长1团员5单买',
              `renshu` int(2) NOT NULL DEFAULT '0' COMMENT '0团①1团②',
              `pid` int(11) NOT NULL DEFAULT '0' COMMENT '团长ID',
              `isfinish` int(2) DEFAULT '0',
              `xunifinish` int(2) DEFAULT '0',
              `hexiaofee` decimal(10,2) DEFAULT '0.00' COMMENT '核销金额',
              `hexiao` int(2) DEFAULT '0' COMMENT '核销状态',
              `tuannum` int(11) DEFAULT '0' COMMENT '团人数',
              `qrimgid` int(11) DEFAULT '0' COMMENT '收到的二维码',
              `qrimg` varchar(300) DEFAULT NULL COMMENT '收到的二维码',
              `isfirstbuy` int(2) DEFAULT '0' COMMENT '1霸王抢0正常',
              `beizhu` text,
              `hexiaoerm` varchar(150) NOT NULL,
              `hexiaogifterm` varchar(150) NOT NULL,
              `uid_old` int(11) DEFAULT '0',
              `openid_old` varchar(32) DEFAULT NULL,
              `fromuid_old` int(11) DEFAULT '0',
              `fopenid_old` varchar(32) DEFAULT NULL,
              `workerid_old` int(11) DEFAULT '0',
              `is_member` int(2) DEFAULT '0' COMMENT '0未标记1是老客户2是新客户',
              `chongzhi` decimal(10,2) DEFAULT '0.00',
              `member_beizhu` text,
              PRIMARY KEY (`id`),
              UNIQUE KEY `uncode` (`uncode`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;


            -- CREATE TABLE  IF NOT EXISTS `yx_yx18900_areply` (
            -- `id` int(11) NOT NULL AUTO_INCREMENT,
            --   `uniacid` int(11) DEFAULT '0',
            --   `themeid` int(11) DEFAULT '0',
            --   `nickname` varchar(100) DEFAULT NULL,
            --   `avatar` varchar(300) DEFAULT NULL,
            --   `type` int(2) DEFAULT '0',
            --   `uid` int(11) DEFAULT '0',
            --   `openid` varchar(32) DEFAULT NULL,
            --   `toid` int(11) DEFAULT '0',
            --   `touid` int(11) DEFAULT '0',
            --   `toopenid` varchar(100) DEFAULT NULL,
            --   `content` text,
            --   `imgs` text,
            --   `addtime` int(11) DEFAULT '0',
            --   `status` int(2) DEFAULT '0',
            --   PRIMARY KEY (`id`)
            -- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;


            CREATE TABLE IF NOT EXISTS `yx_yx18900_afans` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) DEFAULT '0',
              `uid` int(11) DEFAULT '0',
              `openid` varchar(32) DEFAULT NULL,
              `unionid` varchar(32) DEFAULT NULL,
              `nickname` varchar(100) DEFAULT NULL,
              `avatar` varchar(300) DEFAULT NULL,
              `username` varchar(50) NOT NULL,
              `mobile` varchar(50) NOT NULL,
              `workerid` int(11) DEFAULT '0',
              `jihui` int(11) DEFAULT '0',
              `option` varchar(300) NOT NULL,
              `jiagetype` varchar(300) NOT NULL,
              `address` varchar(300) DEFAULT NULL,
              `formtwo` varchar(300) DEFAULT NULL,
              `datetime` varchar(100) NOT NULL,
              `addinfo` varchar(100) NOT NULL,
              `poster` varchar(150) NOT NULL,
              `poster2` varchar(150) NOT NULL,
              `poster_qrcode` varchar(150) NOT NULL,
              `postertime` int(11) DEFAULT '0',
              `fromuid` int(11) DEFAULT '0',
              `friendnum` int(11) DEFAULT '0',
              `friendshiyong` int(11) DEFAULT '0',
              `fopenid` varchar(32) DEFAULT NULL,
              `themeid` int(11) DEFAULT '0',
              `ip` varchar(50) DEFAULT NULL,
              `ipaddress` varchar(300) DEFAULT NULL,
              `addtime` int(11) DEFAULT '0',
              `checkicon` int(2) DEFAULT '0',
              `status` int(2) DEFAULT '0' COMMENT '4同城检查员 3手工白名单 2检查员 1正常 0黑名单、可能同行 -1此服务器投诉过 -2此链接投诉过 -3百分百同行 -4同城同行 -5访问投诉者的链接',
              `islock` int(2) DEFAULT '0',
              `sublive` int(11) DEFAULT '0',
              `msgstatus` int(2) DEFAULT '0',
              `scansum` int(11) DEFAULT '0',
              `forward` int(11) DEFAULT '0',
              `feesum` decimal(10,2) DEFAULT '0.00',
              `commcount` int(11) DEFAULT '0',
              `feesumxuni` decimal(10,2) DEFAULT '0.00',
              `commcountxuni` int(11) DEFAULT '0',
              `lightsum` int(11) DEFAULT '0',
              `tuijian` int(11) DEFAULT '0',
              `responsum` int(11) DEFAULT '0',
              `tuijianxuni` int(11) DEFAULT '0',
              `responsumxuni` int(11) DEFAULT '0',
              `uid_old` int(11) DEFAULT '0',
              `openid_old` varchar(32) DEFAULT NULL,
              `fromuid_old` int(11) DEFAULT '0',
              `fopenid_old` varchar(32) DEFAULT NULL,
              `workerid_old` int(11) DEFAULT '0',
              `hdsourceurl` varchar(300) DEFAULT NULL,
              `hexiaocode` varchar(100) NOT NULL,
              `login_status` int(2) DEFAULT '0',
              `adshow` int(11) DEFAULT '0',
              `lt_points` int(11) DEFAULT '0' COMMENT '抽奖积分',
              `lt_pointsxuni` int(11) DEFAULT '0' COMMENT '抽奖积分',
              `lt_counter` int(11) DEFAULT '0' COMMENT '抽奖剩余次数',
              `click_open_box` int(2) DEFAULT '0' COMMENT '是否报名拆盲盒',
              `wechatgroupid` int(11) DEFAULT '0' COMMENT '微信群ID',
              `wechatgroupidold` varchar(100) NOT NULL COMMENT '微信群ID用过的',
              `ad_info` text  comment '广告点击次数',
              `other_info` text comment '粉丝其它信息',
              `is_receiver` int(2) DEFAULT '0' COMMENT '是否添加分帐',
              `info` json DEFAULT NULL COMMENT '粉丝其它信息',
              PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;


            CREATE TABLE IF NOT EXISTS `yx_yx18900_ashare` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) DEFAULT '0',
              `themeid` int(11) DEFAULT '0',
              `uid` int(11) DEFAULT '0',
              `openid` varchar(32) DEFAULT NULL,
              `addtime` int(11) DEFAULT '0',
              `status` int(2) DEFAULT '0',
              `fromuid` int(11) NOT NULL DEFAULT '0',
              `fopenid` varchar(32) NOT NULL,
              `workerid` int(11) DEFAULT '0',
              `ischeck` int(2) NOT NULL DEFAULT '0',
              `uid_old` int(11) DEFAULT '0',
              `openid_old` varchar(32) DEFAULT NULL,
              `fromuid_old` int(11) DEFAULT '0',
              `fopenid_old` varchar(32) DEFAULT NULL,
              `workerid_old` int(11) DEFAULT '0',
              PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

            CREATE TABLE IF NOT EXISTS `yx_yx18900_awechatgroup` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `themeid` int(11) DEFAULT '0',
              `workerid` int(11) DEFAULT '0',
              `name` varchar(32) DEFAULT NULL,
              `img` varchar(300) DEFAULT NULL,
              `count` int(11) DEFAULT '0',
              `addtime` int(11) DEFAULT '0',
              PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

            CREATE TABLE IF NOT EXISTS `yx_yx18900_aorder` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) NOT NULL,
              `type` varchar(50) DEFAULT '0',
              `ordersn` varchar(100) DEFAULT NULL,
              `tid` varchar(100) DEFAULT NULL,
              `uniontid` varchar(100) DEFAULT NULL,
              `transaction_id` varchar(100) DEFAULT NULL,
              `themeid` int(11) DEFAULT '0',
              `ordertype` varchar(100) DEFAULT NULL,
              `masterid` int(11) DEFAULT '0',
              `detailid` int(11) DEFAULT '0',
              `openid` varchar(32) DEFAULT NULL,
              `uid` int(11) DEFAULT '0',
              `username` varchar(100) DEFAULT NULL,
              `mobile` varchar(50) DEFAULT NULL,
              `orderfee` float DEFAULT '0',
              `realfee` float DEFAULT '0',
              `checkfee` float DEFAULT '0',
              `item1fee` float DEFAULT '0',
              `usecard` int(2) DEFAULT '0',
              `usecardid` int(11) DEFAULT '0',
              `status` int(2) NOT NULL,
              `tiaozhuan` int(2) NOT NULL,
              `addtime` int(11) DEFAULT '0',
              `paytime` int(11) DEFAULT '0',
              `refundtime` int(11) DEFAULT '0',
              `paystate` int(2) DEFAULT '0',
              `workerid` int(11) DEFAULT '0',
              `checktime` int(11) DEFAULT '0',
              `ispay` int(2) DEFAULT NULL,
              `profitsharing_fee` float DEFAULT '0',
              `profitsharingfinish` int(2) DEFAULT '0',
              `uid_old` int(11) DEFAULT '0',
              `openid_old` varchar(32) DEFAULT NULL,
              `workerid_old` int(11) DEFAULT '0',
              PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

            CREATE TABLE  IF NOT EXISTS `yx_yx18900_ateam` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) DEFAULT '0',
              `groupid` int(11) DEFAULT '0',
              `name` varchar(300) DEFAULT NULL,
              `company` int(11) DEFAULT '0',
              `scansum` int(11) DEFAULT '0',
              `forward` int(11) DEFAULT '0',
              `paycount` int(11) DEFAULT '0',
              `paysum` int(11) DEFAULT '0',
              `unpaidcount` int(11) DEFAULT '0',
              `unpaidsum` int(11) DEFAULT '0',

              `state` int(2) DEFAULT '0',

              PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

            CREATE TABLE  IF NOT EXISTS `yx_yx18900_aworker` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) NOT NULL,
              `type` varchar(50) DEFAULT NULL,
              `themeid` int(11) DEFAULT '0',
              `uid` int(11) DEFAULT '0',
              `openid` varchar(32) DEFAULT NULL,
              `ordersum` int(11) DEFAULT '0',
              `addtime` int(11) DEFAULT '0',
              `status` int(2) DEFAULT '0',
              `username` varchar(50) DEFAULT NULL,
              `wechat` varchar(50) DEFAULT NULL,
              `mobile` varchar(50) DEFAULT NULL,
              `team` int(11) DEFAULT '0',
              `company` int(11) DEFAULT '0',
              `qr` varchar(100) DEFAULT NULL,
              `scansum` int(11) DEFAULT '0',
              `forward` int(11) DEFAULT '0',
              `view_data_auth` int(2) DEFAULT '0',
              `download_auth` int(2) DEFAULT '0',
              `hexiao_auth` int(2) DEFAULT '0',
              `download_key` varchar(64) NULL COMMENT '数据下载key',
              `download_key_time` int(11) NULL COMMENT '数据下载过期时间',
              `nickname` varchar(100) DEFAULT NULL,
              `avatar` varchar(300) DEFAULT NULL,
              `level` int(11) DEFAULT '0',
              `directcount` int(11) DEFAULT '0',
              `directsum` float DEFAULT '0',
              `paycount` int(11) DEFAULT '0',
              `paysum` float DEFAULT '0',
              `unpaidcount` int(11) DEFAULT '0',
              `commcount` int(11) DEFAULT '0',
              `commsum` float DEFAULT '0',
              `commfail` int(11) DEFAULT '0',
              `paycounttoday` int(11) DEFAULT '0',
              `unpaidcounttoday` int(11) DEFAULT '0',
              `directcounttoday` int(11) DEFAULT '0',
              `comm4status` int(2) DEFAULT '0',
              `qrcode` text ,
              `qruserid` int(11) DEFAULT '0',
              `uid_old` int(11) DEFAULT '0',
              `openid_old` varchar(32) DEFAULT NULL,
              `hexiaoerm` varchar(150) NOT NULL,
              `hexiaogifterm` varchar(150) NOT NULL,
              `qr_agent` varchar(150) NOT NULL,
              `poster` varchar(150) NOT NULL,
              `superior` int(11) DEFAULT '0',
              `refunderm` varchar(150) NOT NULL,
              `quick_input` json NULL COMMENT '快捷输入',
              PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

            CREATE TABLE IF NOT EXISTS `yx_yx18900_ahexiao` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) DEFAULT '0',
              `order_id` int(11) DEFAULT '0',
              `hxtype` int(2) DEFAULT '0' COMMENT '0定单核销 1礼品核销',
              `uid` int(11) DEFAULT '0',
              `shopid` int(11) DEFAULT '0',
              `addtime` datetime DEFAULT NULL COMMENT '时间',
              `themeid` int(11) DEFAULT '0',
              `workerid` int(11) DEFAULT '0',
              `uid_old` int(11) DEFAULT '0',
              `workerid_old` int(11) DEFAULT '0',
              `other` text,
              PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;


            CREATE TABLE IF NOT EXISTS `yx_yx18900_alog` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) DEFAULT '0',
              `themeid` int(11) DEFAULT '0',
              `type` varchar(100) DEFAULT NULL,
              `desc` text,
              `tuwen` text,
              `user` varchar(100) DEFAULT NULL,
              `url` varchar(300) DEFAULT NULL,
              `ip` varchar(30) DEFAULT NULL,
              `addtime` int(11) DEFAULT '0',
              PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;


            CREATE TABLE  IF NOT EXISTS `yx_yx18900_ashangjia` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uniacid` int(11) DEFAULT '0',
              `displayorder` int(11) DEFAULT '0',
              `groupid` int(11) DEFAULT '0',
              `fansid` int(11) DEFAULT '0',
              `openid` varchar(32) DEFAULT NULL,
              `name` varchar(300) DEFAULT NULL,
              `address` varchar(300) DEFAULT NULL,
              `mobile` varchar(100) DEFAULT NULL,
              `mobile1` varchar(100) DEFAULT NULL,
              `workerpass` varchar(30) DEFAULT NULL,
              `shoppass` varchar(30) DEFAULT NULL,
              `guanli` int(11) DEFAULT '0',
              `jiami` varchar(100) DEFAULT NULL,
              `xunizhuanfa` int(11) DEFAULT '0',
              `xuniliulan` int(11) DEFAULT '0',
              `xunijinri` int(11) DEFAULT '0',
              `xunichengjiao` int(11) DEFAULT '0',
              `state` int(2) DEFAULT '0',
              `content` text,
              `darkicon` varchar(300) DEFAULT NULL,
              `imgs` varchar(300) DEFAULT NULL,
              `nickname` varchar(300) DEFAULT NULL,
              `headimgurl` varchar(300) DEFAULT NULL,
              `logo` varchar(300) DEFAULT NULL,
              `topimgs` text,
              `video` varchar(300) DEFAULT NULL,
              `videoyouku` varchar(300) DEFAULT NULL,
              `videotype` int(2) DEFAULT '0',
              `text1` varchar(300) DEFAULT NULL,
              `text2` varchar(300) DEFAULT NULL,
              `text3` varchar(300) DEFAULT NULL,
              `text4` varchar(300) DEFAULT NULL,
              `scansum` int(11) DEFAULT '0',
              `forward` int(11) DEFAULT '0',
              `paycount` int(11) DEFAULT '0',
              `paysum` int(11) DEFAULT '0',
              `unpaidcount` int(11) DEFAULT '0',
              `unpaidsum` int(11) DEFAULT '0',
              `url` varchar(300) DEFAULT NULL,
              `xuanze` int(11) DEFAULT '0',
              `liulan` int(11) DEFAULT '0',
              `xuanze_xn` int(11) DEFAULT '0',
              `liulan_xn` int(11) DEFAULT '0',


              PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

            CREATE TABLE IF NOT EXISTS `yx_yx18900_ad` (
            `id` int(11) not null AUTO_INCREMENT,
            `adimg` varchar(256) not null default '' comment '弹窗广告图',
            `adurl` varchar(128) not null default '' comment '弹屏超链',
            `adbegintime` int(11) not null default 0 comment '弹窗开始时间',
            `adendtime` int(11) not null default 0 comment '弹窗结束时间',
            `adshow` int(2) not null default 0 comment '显示位置',
            `adsize` int(4) not null default 100 comment '显示大小',
            `adshownum` int(4) not null default 1 comment '同一用户出现几次',
            `aduser` int(2) not null default 0 comment '用户类型',
            `quantity` int(2) not null default 0 comment '用户数',
            `sort` int(11) not null default 10 comment '排序 小靠前',
            `created_at` timestamp default CURRENT_TIMESTAMP comment '创建时间',
            `updated_at` timestamp default '0000-00-00 00:00:00' comment '更新时间',
            `themeid` varchar(11) not null default '' comment '活动id',
             `other` text,
            PRIMARY KEY (`id`)
            )ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='广告表';


            CREATE TABLE `yx_yx18900_userdata_all` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `themeid` int(11) NOT NULL,
              `uid` int(11) NOT NULL,
              `from_uid` int(11) DEFAULT '0' COMMENT '最后上级',
              `from_uids` text COMMENT '浏览过的用户',
              `workerid` int(11) NOT NULL COMMENT '最后归属员工',
              `workerids` text NOT NULL COMMENT '归属过的员工',
              `is_pay` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否支付',
              `pay_time` timestamp NULL DEFAULT NULL,
              `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
              `typesnum` json DEFAULT NULL COMMENT '类型数量汇总',
              `path_id` int(11) DEFAULT '0' COMMENT '最后来源路径id',
              `sign_type` tinyint(1) NOT NULL DEFAULT '0' COMMENT '标记类型 1未标记 2意向 3潜在 4无意向',
              `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
              `collect_data` json DEFAULT NULL COMMENT '汇总数据 ',
              `trace_next_time` timestamp NULL DEFAULT NULL COMMENT '追踪下次提醒时间',
              `trace_last_time` timestamp NULL DEFAULT NULL COMMENT '最后提交追踪时间',
              PRIMARY KEY (`id`),
              UNIQUE KEY `unq_themeid_uid` (`themeid`,`uid`) USING HASH,
              KEY `normal_uid` (`uid`) USING BTREE,
              KEY `normal_workerid` (`workerid`) USING BTREE
            ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


            CREATE TABLE `yx_yx18900_userdata_record` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `themeid` int(11) NOT NULL,
              `link_type` int(11) NOT NULL COMMENT '链接类型 1好友/群 2朋友圈 3海报',
              `uid` int(11) NOT NULL,
              `from_uid` int(11) NOT NULL,
              `type` int(4) NOT NULL COMMENT '动作类型',
              `type_num` int(5) NOT NULL COMMENT '动作次数',
              `workerid` int(11) NOT NULL DEFAULT '0',
              `path_id` int(11) DEFAULT '0',
              `create_time` char(19) NOT NULL,
              `remark` varchar(255) DEFAULT NULL COMMENT '备注',
              `post` json DEFAULT NULL,
              `userdata_record_id` int(11) NOT NULL DEFAULT '0',
              `url` varchar(300) DEFAULT NULL COMMENT '当前网址',
              `standing_time` int(11) DEFAULT '0' COMMENT '停留时长',
              PRIMARY KEY (`id`),
              KEY `nor_uid` (`uid`) USING BTREE,
              KEY `nor_workerid` (`workerid`) USING BTREE,
              KEY `nor_fromuid` (`from_uid`) USING BTREE
            ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


            CREATE TABLE `yx_yx18900_userdata_trace` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `uid` int(11) NOT NULL,
              `workerid` int(11) DEFAULT NULL,
              `themeid` int(11) DEFAULT NULL,
              `sign_type` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1未标记 2意向客户 3潜在客户 4无意向客户',
              `content` varchar(300) DEFAULT NULL,
              `next_time` timestamp NULL DEFAULT NULL COMMENT '下次回访提醒',
              `create_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
              PRIMARY KEY (`id`)
            ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


            CREATE TABLE `yx_yx18900_userdata_relation` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `themeid` int(11) NOT NULL,
              `from_uid` int(11) NOT NULL,
              `workerid` int(11) NOT NULL,
              `path` text,
              `pid` int(11) DEFAULT NULL,
              `from_userdata_rrecord_id` varchar(11) DEFAULT NULL,
              `recordid` varchar(11) DEFAULT '0',
              `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
              `data` json DEFAULT NULL,
              `updata_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
              PRIMARY KEY (`id`)
            ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

            CREATE TABLE `yx_yx18900_fans_other_form` (
                `id` int(11) not null AUTO_INCREMENT,
                `uid` int(11)  null   comment '用户uid',
                `fileds` json  comment '填写信息',
                `themeid` int(11)  null  comment '活动id',
                `workerid` int(11)  null  comment '绑定员工id',
                `fromuid` int(11)  null  comment '当前状态下fromuid',
                `created_at` timestamp default '0000-00-00 00:00:00' comment '创建时间',
                `updated_at` timestamp default CURRENT_TIMESTAMP comment '更新时间',
                PRIMARY KEY (`id`)
            ) ENGINE = InnoDB DEFAULT CHARACTER SET =utf8 COMMENT='客资收集表';

            ALTER TABLE `yx_yx18900_afans` ADD INDEX rds_idx_0 (`uid`, `themeid`);
            ALTER TABLE `yx_yx18900_afans` ADD INDEX rds_idx_1 (`openid`, `themeid`);
            ALTER TABLE `yx_yx18900_afans` ADD UNIQUE rds_idx_2 (`openid`, `themeid`);
            ALTER TABLE `yx_yx18900_afans` ADD INDEX `idx_workerid_themeid_addtime` (`workerid`, `themeid`, `addtime`);
            ALTER TABLE `yx_yx18900_afans` ADD INDEX `idx_login_status_themeid_uid` (`login_status`, `themeid`, `uid`);
            ALTER TABLE `yx_yx18900_aworker` ADD UNIQUE KEY `rds_idx_0`(`themeid`,`uid`);

            ALTER TABLE `yx_yx18900_acomm` ADD INDEX rds_idx_0 (`status`);
            ALTER TABLE `yx_yx18900_acomm` ADD INDEX rds_idx_1 (`msg`, `uniacid`, `type`);
            ALTER TABLE `yx_yx18900_acomm` ADD INDEX rds_idx_2 (`status`, `uniacid`, `themeid`, `addtime`);


            ALTER TABLE `yx_yx18900_arecord` ADD INDEX rds_idx_0 (`themeid`, `status`);
            ALTER TABLE `yx_yx18900_arecord` ADD INDEX rds_idx_1 (`openid`, `type`, `themeid`, `status`);
            ALTER TABLE `yx_yx18900_arecord` ADD INDEX rds_idx_2 (`type`, `isfinish`, `themeid`, `status`);
            ALTER TABLE `yx_yx18900_arecord` ADD INDEX rds_idx_3 (`type`, `themeid`, `status`);
            ALTER TABLE `yx_yx18900_arecord` ADD INDEX `idx_workerid_themeid_addtime_status` (`workerid`, `themeid`, `addtime`, `status`);

            ALTER TABLE `yx_yx18900_aorder` ADD INDEX rds_idx_1 (`openid`);

            ALTER TABLE `yx_yx18900_arelation` ADD INDEX rds_idx_1 (`openid`, `themeid`);
            ALTER TABLE `yx_yx18900_ascan` ADD INDEX rds_idx_1 (`openid`, `uniacid`, `themeid`);

            ALTER TABLE `yx_yx18900_ascan` ADD INDEX `idx_fopenid_themeid_status_openid` (`fopenid`, `themeid`, `status`, `openid`);
            ALTER TABLE `yx_yx18900_acommshare` ADD INDEX `idx_uid_status_themeid` (`uid`, `status`, `themeid`);
            ALTER TABLE `yx_yx18900_acomm` ADD INDEX `idx_status_themeid_fee` (`status`, `themeid`, `fee`);
            ALTER TABLE `yx_yx18900_acomm` ADD INDEX `idx_status_themeid_uid_addtime_type_fee` (`status`, `themeid`, `uid`, `addtime`, `type`, `fee`);
            ALTER TABLE `yx_yx18900_arelation` ADD INDEX `idx_uid` (`uid`);
            ALTER TABLE `yx_yx18900_ascan` ADD INDEX `idx_uid` (`uid`);
            ALTER TABLE `yx_yx18900_acomm` ADD INDEX `idx_status_themeid_addtime_type_uid_fee` (`status`, `themeid`, `addtime`, `type`, `uid`, `fee`);
            ALTER TABLE `yx_yx18900_aorder` ADD INDEX `idx_tid` (`tid`);
            ALTER TABLE `yx_yx18900_aworkerreport` ADD INDEX `idx_workerid` (`workerid`);
            ";
        $sql = str_replace("yx18900", $modulename, $sql);

        db_run($sql);
    }


}